package com.zhuge_new.Fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.makeramen.roundedimageview.RoundedImageView;
import com.maning.mndialoglibrary.MToast;
import com.zhuge_new.Adapter.Redexamine1Adapter;
import com.zhuge_new.Base.BaseFragment;
import com.zhuge_new.R;
import com.zhuge_new.bean.RedexanminBean;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.DataParse;
import com.zhuge_new.untils.NumberUtil;
import com.zhuge_new.untils.PreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class RedexanmineFragmen1 extends BaseFragment {

    private RecyclerView redex1_list;
    private View head;
    private RoundedImageView redxanmine_logo;
    private TextView redxanmin_money;
    private RedexanminBean redexanminBean;
    private TextView red_num;
    private Redexamine1Adapter redexamine1Adapter;
    private boolean  refresh=true;    //控制下拉刷新


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_redexanmine, container, false);
        head = inflater.inflate(R.layout.redxanmine_head, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init_title();
        init_getData(pageNum);
    }

    @Override
    public void init_title() {
        super.init_title();
        redex1_list = getActivity().findViewById(R.id.redex1_list);
        redex1_list.setLayoutManager(new LinearLayoutManager(getActivity()));
        red_num = head.findViewById(R.id.red_num);
        redxanmine_logo = head.findViewById(R.id.redxanmine_logo);
        redxanmin_money = head.findViewById(R.id.redxanmin_money);

        SpannableStringBuilder builder = new SpannableStringBuilder(red_num.getText());
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(ContextCompat.getColor(getActivity(), R.color.red_red));
        builder.setSpan(foregroundColorSpan, 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }


    private void init_getData(int pageNum) {
        OkGo.<String>post(BaseHttp.selGetRedpacketLists)
                .tag(this)
                .params("OpenId", PreferencesUtils.getString(getActivity(), "unionid"))
                .params("rows", "15")
                .params("page", pageNum)
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        redexanminBean = (RedexanminBean) new DataParse().getBean(response.body(), RedexanminBean.class, 1);
                        red_num.setText("收到"+redexanminBean.getNum()+"个红包，共计");
                        redxanmin_money.setText(redexanminBean.getMoneySum());
                        redexamine1Adapter = new Redexamine1Adapter(getActivity(), redexanminBean);
                        redexamine1Adapter.addHeaderView(head);
                        redexamine1Adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                            @Override
                            public void onLoadMoreRequested() {
                                if (refresh){
                                    loadMore();
                                }
                            }
                        }, redex1_list);
                        redex1_list.setAdapter(redexamine1Adapter);

                    }
                });
    }

    private void loadMore() {
        pageNum = pageNum + 1;
        OkGo.<String>post(BaseHttp.selGetRedpacketLists)
                .tag(this)
                .params("OpenId", PreferencesUtils.getString(getActivity(), "unionid"))
                .params("rows", "15")
                .params("page", pageNum)
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        redexanminBean = (RedexanminBean) new DataParse().getBean(response.body(), RedexanminBean.class, 1);
                        redexamine1Adapter.addData(redexanminBean.getObject());
                        redexamine1Adapter.loadMoreComplete();
                        if (redexanminBean.getObject().isEmpty()) {
                            refresh=false;
                            pageNum = pageNum - 1;
                            MToast.makeTextShort(getActivity(),"已经到底部了");
                        }
                    }
                });
    }

}
